// @flow
import React, { Component } from 'react'
import { AgGridReact } from 'ag-grid-react'
import styles from './DictSettingGrid.css'
import DictSettingActions from './DictSettingActions'
import Pagination from 'components/Pagination'
import universalFetch, { handleFetchError } from 'store/modules/fetch'

type Props = {
  history: Object,
  pageSize: number,
  current: number,
  total: number,
  rowData: Array<Object>,
  getData: Function
}
type States = {
  rowData: Array<Object>,
  columnDefs: Array<Object>,
  total: number,
  current: number
}
class DictSettingGrid extends Component {
  props: Props
  state: States
  gridOptions: Object
  onGridReady: Function
  getDictSetting: Function
  api: Object
  constructor (props: Props) {
    super(props)
    this.state = {
      rowData: [],
      columnDefs: [
        {
          headerName: '显示名称',
          field: 'label',
          cellStyle: {
            'padding': '8px'
          }
        },
        {
          headerName: '回填内容',
          field: 'value',
          cellStyle: {
            padding: '8px'
          }
        },
        {
          headerName: '字典类型',
          field: 'type',
          cellStyle: {
            padding: '8px'
          }
        },
        {
          headerName: '类型描述',
          field: 'typeCn',
          cellStyle: {
            padding: '8px'
          }
        },
        // {
        //   headerName: '扩展属性',
        //   field: 'ext',
        //   cellStyle: {
        //     padding: '8px'
        //   }
        // },
        {
          headerName: '显示顺序',
          field: 'sort',
          cellStyle: {
            padding: '8px'
          }
        },
        {
          headerName: '操作',
          cellRendererParams: {
            history: this.props.history,
            refreshDictSetting: this.getDictSetting
          },
          cellRendererFramework: DictSettingActions,
          cellStyle: {
            padding: '8px',
            'text-align': 'center'
          }
        }
      ],
      total: 1,
      current: 1
    }
    this.gridOptions = {
      localeText: {
        loadingOoo: '数据加载中...',
        noRowsToShow: '暂无数据',
        filterOoo: '搜索当前页面',
        equals: '等于',
        notEqual: '不等于',
        contains: '包含',
        startsWith: '开始于',
        endsWith: '结束于',
        copy: '复制',
        paste: '粘贴',
        copyWithHeaders: '带表头复制',
        toolPanel: '工具栏',
        export: '导出',
        csvExport: '导出CSV',
        excelExport: '导出Excel'
      },
      enableSorting: true,
      enableFilter: true,
      rowSelection: 'single',
      rowDeselection: true,
      enableColResize: true
    }
    this.onGridReady = this.onGridReady.bind(this)
    this.getDictSetting = this.getDictSetting.bind(this)
  }

  componentWillReceiveProps (nextProps: Object) {
    this.setState({
      total: nextProps.total,
      current: nextProps.current,
      rowData: nextProps.rowData
    })
  }

  // componentDidMount () {
  //   const { pageSize } = this.props
  //   this.getDictSetting(1, pageSize)
  // }

  onGridReady (params: Object) {
    this.api = params.api
    this.api.sizeColumnsToFit()
  }

  getDictSetting (page: number, pageSize: number) {
    if (page == null) {
      this.props.getData(1, pageSize)
    } else {
      this.props.getData(page, pageSize)
    }
  }
  render () {
    const { columnDefs, rowData, current, total } = this.state
    const { pageSize } = this.props
    return (
      <div className={styles['grid'] + ' ag-fresh'}>
        <AgGridReact {...{
          gridOptions: this.gridOptions,
          rowHeight: '34',
          headerHeight: '28',
          columnDefs: columnDefs,
          rowData: rowData,
          onGridReady: this.onGridReady
        }}
          />
        <Pagination
          total={total}
          current={current} pageSize={pageSize} onChange={this.getDictSetting} />
      </div>
    )
  }
}

export default DictSettingGrid
